package com.vogue.supplymall.account.domain.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vogue.ssm.database.dao.BaseDao;
import com.vogue.supplymall.account.domain.dao.AddressDao;
import com.vogue.supplymall.account.domain.model.Address;
import com.vogue.supplymall.account.domain.service.AddressService;
import com.vogue.supplymall.common.constant.ConstantBean.EnumLoginType;
import com.vogue.supplymall.common.domain.tool.MessageSourceUtils;
import com.vogue.supplymall.common.exception.DataNotFindException;

/**
 * Created by 崔岳 on 2018-1-10.
 */
@Service("addressService")
public class AddressServiceImpl extends BaseServiceImpl<Address, Long> implements AddressService {

	private AddressDao addressDao;

	@Autowired
	protected MessageSourceUtils messageSourceUtils;
	
	public AddressServiceImpl(BaseDao<Address, Long> baseDao) {
		super(baseDao);
		addressDao = (AddressDao) baseDao;
	}
	private static String ADDRESS_LIST_SQL_WHERE = "syscodeid=%d and organid=%d and userid=%d order by defaultflag desc,createdate";
	
	private static String ADDRESS_LIST_SQL_WHERE_NOPERSON = "syscodeid=%d and organid=%d order by defaultflag desc,createdate";
	
	@Override
	public List<Address> queryList(Long syscodeid, Long organid, Long userid) {
		return this.selectByWhr(String.format(ADDRESS_LIST_SQL_WHERE, syscodeid, organid, userid));
	}

	@Override
	public List<Address> queryList(Long syscodeid, Long organid) {
		return this.selectByWhr(String.format(ADDRESS_LIST_SQL_WHERE_NOPERSON, syscodeid, organid));
	}
	
	@Override
	@Transactional
	public void setupDefault(Long id, Long userid, int userType) {
		// TODO Auto-generated method stub
		if (EnumLoginType.PERSON.getCode() == userType) {
			addressDao.update("update address set defaultflag=0 "
					+ "where type<>4 and userid=" + userid);
			Address address = addressDao.get(id);
			if(address == null) {
				throw new DataNotFindException(
						messageSourceUtils.getMessage(
								com.vogue.supplymall.common.constant.ConstantBean.GLOBAL_SYSTEM
								+ "addr", null));
			}
			address.setDefaultflag(true);
			addressDao.update(address);
		}
	}

	@Override
	@Transactional
	public void setupDefaultForOrgan(Long id, Long organid) {
		// TODO Auto-generated method stub
		addressDao.update("update address set defaultflag=0 "
				+ "where type=4 and organid=" + organid);
		Address address = addressDao.get(id);
		if(address == null) {
			throw new DataNotFindException(
					messageSourceUtils.getMessage(
							com.vogue.supplymall.common.constant.ConstantBean.GLOBAL_SYSTEM
							+ "addr", null));
		}
		address.setDefaultflag(true);
		addressDao.update(address);
	}
	
	@Override
	@Transactional
	public void saveOrUpdate(Address address) {
		// TODO Auto-generated method stub
		if(address.getDefaultflag()) {
			addressDao.update("update address set defaultflag=0 "
					+ "where type<>4 and userid=" + address.getUserid());
		}
		if(address.getId() == null || address.getId() == 0L) {
			addressDao.save(address);
		}else {
			addressDao.update(address);
		}		
	}
}
